$(function() {
	//'{"total" : '.$rowTotal.', "rows" : '.json_encode($array).'}';
	//初始化网格数据
	dataGrid('Event_userId');
	//监听修改状态
	$('#updateStatus').change(function(){
		$.ajax({
			url:'Event_status',
			type:'post',
			data:'eventId='+$('#eventId').val()+'&&eventStatus='+$(this).val(),
			beforeSend:function(){
				ajaxLoading("拼命修改中...");
			},
			success:function(data){
				closeMsg(0);
				if (!data.RESULT) {
					showMsg("修改失败", 0);
				} else {
					$.messager.alert("操作提示","<font size=\"4\">修改成功</font>","info",function(){
						$('#event').datagrid('reload'); 
					});
				}
			}
		});
	});
});

//表格数据
function dataGrid(u){
	$('#event').datagrid({
		url : u,
		title : '所有事件内容',
		loadMsg : '拼命加载中...',
		iconCls : 'icon-search',
		width : '100%',
		height : 360,
		toolbar : '#toolbar',
		columns : [ [ 
		{
			field : 'EVENTID',
			title : '事件号',
			width : 160,
			align:'center',
			sortable:'true'
		},
		{
			field:'REPORTERNAME',
			title:'报案人姓名',
			width:80,
			align:'center',
			sortable:'true'
		}, 
		{
			field:'REPORTERID',
			title:'报案人学号',
			width:100,
			align:'center',
			sortable:'true'
		},
		{
			field:'REPORTERPHONENUM',
			title:'报案人电话',
			width:100,
			align:'center',
			sortable:'true'
		},
		{
			field:'REPORTERMAIL',
			title:'报案人邮箱',
			width:120,
			align:'center',
			sortable:'true'
		},
		{
			field:'CLASSNAME',
			title:'报案人班级',
			width:120,
			align:'center',
			sortable:'true'
		},
		{
			field:'REPORTERDIRECTORNAME',
			title:'所属辅导员',
			width:80,
			align:'center',
			sortable:'true'
		},
		{
			field:'REPORTERMONITORNAME',
			title:'所属班主任',
			width:80,
			align:'center',
			sortable:'true'
		},
		{
			field:'EVENTDATE',
			title:'报案日期',
			width:80,
			align:'center',
			sortable:'true'
		},
		{
			field : 'EVENTTYPE',
			title : '求助类型',
			width : 80,
			align : 'center',
			sortable:'true'
		},
		{
			field : 'EVENTSTATUS',
			title : '状态',
			width : 80,
			align : 'center',
			sortable:'true',
			editor: 'combobox' 
		}
		]],
		singleSelect : true,
		pageSize : 10,
		pageList : [ 10, 20, 30 ],
		pageNumber : 1,
		sortName : 'EVENTID',
		sortOrder : 'desc',
		pagination : true,//分页控件 
		rownumbers:true,//行号  
		//双击执行的操作
		onDblClickRow:function(){
			editStatus();
		}
	});
}

//高级搜索
function heightFind(value,name){
	dataGrid('Event_search?keyWord='+value);
}

//事件号
var eId = '';
//查看基本信息
function editStatus(){
	var rows = $('#event').datagrid('getSelected');
	if(rows){
		eId = rows.EVENTID;
		//加载基本信息
		$('#fm').form('load',rows);
		$('#uploadImage').form('load',{
			eventId:rows.EVENTID
		});
		$('#uploadAudio').form('load',{
			eventId:rows.EVENTID
		});
		//图片信息
		reloadImage();
		//录音信息
		reloadAudio();
		$('#editStatus').dialog('setTitle',"查看详情信息(<font color=red>"+rows.EVENTID+"</font>)").dialog('open');
	}else{
		showMsg("请选择一条记录进行编辑",0);
	}
}

//刷新图片页面
function reloadImage(){
	$.get("Image_exist?eventId="+eId,function(json){
		var str='';
		if(json!=null){
			str = str + "<ul>";
			$.each(json,function(i,n){
				str = str + "<li><a target=\"_Blank\"href=\"Image_get?eventId="+eId+"&&id="+n.IMAGEID+"\" title=\"点击查看图片\">"+n.IMAGENAME+"</a></li>";
			});
			str = str + "</ul>";
			$('#imageBox').html(str);
		}
	},'json');
}

//刷新录音列表
function reloadAudio(){
	$.get("Audio_exist?eventId="+eId,function(json){
		var str='';
		if(json!=null){
			str = str + "<ul>";
			$.each(json,function(i,n){
				//target=\"_Blank\"href=\"Audio_get?eventId="+eId+"&&id="+n.AUDIOID+"\" title=\"点击播放音乐\"
				str = str + "<li><a title=\"点击播放音乐\" onclick=\"audioPlayer('"+eId+"','"+n.AUDIOID+"')\">"+n.AUDIONAME+"</a></li>";
			});
			str = str + "</ul>";
			$('#audioBox').html(str);
		}
	},'json');
}

//播放录音
function audioPlayer(eId,aId){
	if(checkPlayer()){
		//html5播放器
		$('#statrAudio').html("<audio src=\"Audio_get?eventId="+eId+"&&id="+aId+"\" controls=\"controls\"></audio><iframe id=\"tmp_downloadhelper_iframe\" style=\"display: none;\"></iframe>");
	}else{
		//微软播放器
		$('#statrAudio').html("<embed src=\"Audio_get?eventId="+eId+"&&id="+aId+"\" height=\"50px\" width=\"300px\"pluginspage=\"http://www.microsoft.com/isapi/redir.dll?prd=windows&sbp=mediaplayer&ar=media&sba=plugin&\" type=\"application/x-mplayer2\" border=\"0\" width=\"100%\" height=\"40\" showcontrols=\"1\" showaudiocontrols=\"1\" showstatusbar=\"1\" autostart=\"0\" loop=\"0\" enablecontextmenu=\"1\"></embed>");
	}
	$('#audioPlayer').dialog('open');
}

//根据是否支持浏览器 显示不同的播放器
function checkPlayer(){
		 if (window.applicationCache) {
	         return true;
	     } else {
	         return false;
	     }
}

//============================检测是否为音频或者图片=====================
function checkIsImage(){
	var t = /\.(jpg|jpeg|png)$/;
	var img = $('#imageFile').val();
	if(img==''){
		showMsg('请选择图片上传',0);
		return false;
	}
	if(!t.test(img)){
		showMsg('您选择的不是图片，请选择图片(jpg,jpeg,png)',0);
		return false;
	}
	return true;
}

function checkIsAudio(){
	var t = /\.(mp3|wav)$/;
	var audio = $('#audioFile').val();
	if(audio==''){
		showMsg('请选择录音上传',0);
		return false;
	}
	if(!t.test(audio)){
		showMsg('您选择的不是录音，请选择录音(mp3,wav)',0);
		return false;
	}
	return true;
}
//===========================结束==============================
//上传图片
function uploadImage(){
	if(checkIsImage()){
		$('#uploadImageLoading').html("<img src=\"images/upload.gif\" width=\"150\" height=\"15\"/>");
	$('#uploadImage').form('submit',{
		url:'Image_add',
		onBeforeLoad : function() {
			ajaxLoading("拼命上传中...");
		},
		onSubmit : function() {
			return $(this).form('validate');
		},
		success : function(data) {
			var obj = eval('(' + data + ')');
			if (!obj.RESULT) {
				$('#uploadImageLoading').html("<img src=\"images/error.png\"/>&nbsp;<font color=red>上传失败！</font>");
			} else {
				$('#uploadImageLoading').html("<img src=\"images/success.gif\"/>&nbsp;<font color=red>上传成功！</font>");
				reloadImage();
			}
			$('#uploadImage').form('clear');
			setTimeout(function(){
				$('#uploadImageLoading').html('');
			},5000);
		}
	});
	}
}

//上传录音
function uploadAudio(){
	if(checkIsAudio()){
		$('#uploadAudioLoading').html("<img src=\"images/upload.gif\" width=\"150\" height=\"15\"/>");
		$('#uploadAudio').form('submit',{
			url:'Audio_add',
			onBeforeLoad : function() {
				ajaxLoading("拼命上传中...");
			},
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(data) {
				var obj = eval('(' + data + ')');
				if (!obj.RESULT) {
					$('#uploadAudioLoading').html("<img src=\"images/error.png\"/>&nbsp;<font color=red>上传失败！</font>");
				} else {
					$('#uploadAudioLoading').html("<img src=\"images/success.gif\"/>&nbsp;<font color=red>上传成功！</font>");
					reloadAudio();
				}
				$('#uploadAudio').form('clear');
				setTimeout(function(){
					$('#uploadAudioLoading').html('');
				},5000);
			}
		});
	}
}